#include "stdafx.h"
/******************************************************************************
*
*    Copyright 2002 RoyalTek Company Ltd.
*    All Rights Reserved
*
*******************************************************************************

Description:
	This utility function to use with protocol function.           

 Notes:
******************************************************************************/

#include "ParserUtil.h"
#include <stdio.h>
#include <string.h>

/*-----------------------------------------------------------------------------
Function:       int CleanString(char *pChar, int Length)

Description:    Function to set a character array into empty.

Parameters:     BYTE *Buffer:	the string want to generate checksum.
				int iSize:		size of the buffer.

Return Value:   bool:	true if success, false if failure.
-----------------------------------------------------------------------------*/
BOOL CleanString(char *pChar, int Length)
{
	int i;
	for(i=0; i < Length; i++)
	{
		*pChar = '\0';
		pChar++;
	}
	return true;
}

/*-----------------------------------------------------------------------------
Function:       MSG_RESULT AppendCheckSum( char *pBuf)

Description:    It is the caller function responsibility to ensure enough buffer
				space for the '*CK'  astrisk checksum delimiter and 2 character
				checksum.

Parameters:     char *pBuf	point of the string which will append checksum

Return Value:   MSG_RESULT return.
-----------------------------------------------------------------------------*/
BOOL AsciiAppendCheckSum( char *pBuf)
{
	char ckSum = '\0';

	while( *pBuf != '\0' )
    {
		ckSum ^= *pBuf++;
    }
	sprintf( pBuf, "*%02X", ckSum );  /* +"*CK" */
	return 1;
}

/*-----------------------------------------------------------------------------
Function:       BOOL ASCIIValidChecksum( UINT8 *pData, int Len )

Description:    It is the caller function responsibility to ensure enough buffer
				space for the '*CK'  astrisk checksum delimiter and 2 character
				checksum.

Parameters:     char *pBuf	point of the string which will append checksum

Return Value:   MSG_RESULT return.
-----------------------------------------------------------------------------*/
BOOL AsciiValidChecksum( char *pData, int Len )
{
   char ComputedChksum;
   int MsgChksum;
   int Cnt;
   ComputedChksum = 0;
   Cnt = 0;
   pData++;  /* skip '$' */
   while( (*pData != '*') && (Cnt < Len) )
   {
      ComputedChksum ^= *pData++;
      Cnt++;
   }
   pData++;  /* skip '*' */
   if( 1 != sscanf( (char*) pData, "%02x", &MsgChksum ) )
   {
      return false;
   }
   return ComputedChksum == MsgChksum ? true : false;
}

/*-----------------------------------------------------------------------------
Function:       BOOL AsciiAppendDollarSign(char *pData)

Description:    Append asterisk into the header of the sentence

Parameters:     char *pData:	point of the string which will append asterisk

Return Value:   MSG_RESULT return.
-----------------------------------------------------------------------------*/
BOOL AsciiAppendDollarSign(char *pData)
{
	int i = 0;

	for(i=ASCII_SENTENCE_LENGTH; i > 1; i--)
	{

		*(pData + i - 1) = *(pData + i - 2);
	}
	*pData = '$';
	return true;
}

/*-----------------------------------------------------------------------------
Function:       char* AsciiDeleteHeader(char *pData)

Description:    Delete the header string "$RACKxxx,"

Parameters:     char *pData:	point of the string which will delete header

Return Value:   the result point of string
-----------------------------------------------------------------------------*/
char* AsciiDeleteHeader(char *pData)
{
	pData = pData + 9;
	return pData;
}

/*-----------------------------------------------------------------------------
Function:       BYTE GetCheckSum(BYTE *Buffer, int iSize)

Description:    calculate checksum

Parameters:     BYTE *Buffer:	Point of the data which will append checksum.
				int iSize:	The length of the data.	

Return Value:   MSG_RESULT return.
-----------------------------------------------------------------------------*/
BYTE GetCheckSum(BYTE *Buffer, int iSize)
{
   int  i1;
   BYTE uCheckSum = Buffer [ 0 ];

   for ( i1 = 1; i1 < iSize; i1++ )
      uCheckSum ^= Buffer [ i1 ];

   return uCheckSum;
}

#ifdef WIN32
/*-----------------------------------------------------------------------------
Function:       void userPrintf (char *fmt, ...)

Description:    function for print debug message.

Parameters:     char *fmt, ...:	message string and parameter.

Return Value:   void.
-----------------------------------------------------------------------------*/
void userPrintf (char *fmt, ...)
{
    va_list args;                      /* points to each unnamed arg in turn */
    char      cStr[500];                 /* final, formatted string            */

    /*-----------------------------------------------------------------------*\
     | Use the macro to init args to the 1st unnamed arg                     | 
    \*-----------------------------------------------------------------------*/
    va_start (args, fmt);

    /*-----------------------------------------------------------------------*\
     | Use the variable arg version of sprintf to get the string             | 
    \*-----------------------------------------------------------------------*/
    vsprintf(cStr, fmt, args);

    /*-----------------------------------------------------------------------*\
     | Clean up when done                                                    | 
    \*-----------------------------------------------------------------------*/
    va_end(args);

    /*-----------------------------------------------------------------------*\
     | Print out the formatted string                                        | 
    \*-----------------------------------------------------------------------*/

    /*
     * The following works only in Windows. For other target operating systems,
     * an alternative means of printing must be used.
    */
    #ifdef WIN32
    {
      FILE   *fp;
	  fp = fopen ("c:\\DOWNLOAD.log","ab");
      fwrite (cStr,1,strlen(cStr),fp);
      fwrite ("\r\n",1,2,fp);
      fclose (fp);
    }
    #endif
}
#endif